from flask import Flask, render_template, request
import requests

app = Flask(__name__)

# Telegram Bot Token，替换为您自己的 Token
BOT_TOKEN = '2128654182:AAEuBSuke9XWeHX6djmUR8_vq6H3K9F9oyQ'

# 聊天 ID，替换为您要发送消息的聊天 ID
CHAT_ID = '925990803'

# Telegram 发送消息的 URL
TELEGRAM_URL = f'https://api.telegram.org/bot{BOT_TOKEN}/sendMessage'

@app.route('/receive_message', methods=['GET', 'POST'])  # 允许GET和POST请求
def receive_message():
    if request.method == 'GET':
        text = request.args.get('text')
    elif request.method == 'POST':
        text = request.form.get('text')

    if text:
        # 构建消息
        #text = message

        # 准备要发送的数据
        data = {
            'chat_id': CHAT_ID,
            'text': text
        }

        # 发送消息到 Telegram
        response = requests.post(TELEGRAM_URL, json=data)

        if response.status_code == 200:
            return '发送成功'
        else:
            return '发送失败'
    else:
        return '没有要发送的消息'




@app.route('/')
def index():
    # 渲染 index.html 模板
    return render_template('index.html')

@app.route('/tgindex')
def tgindex():
    # 渲染 tgindex.html 模板
    return render_template('tgindex.html')


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5710)
    #浏览器打开http://ainsof.top:5710/receive_message?text=内容